import axios from 'axios';
import Qs from 'qs'

let util = {};
util.title = function(title) {
    title = title ? title + ' - 金电智诚' : '金电智诚';
    window.document.title = title;
};

util.domain = process.env.NODE_ENV === 'development' ? '' : '';

util.ajax = axios.create({
    baseURL: '',
    timeout: 2000,
    // headers: {'content-type':'application/json;charset=UTF-8'},
    headers: {'Content-Type':'application/x-www-form-urlencoded',},

    responseType: 'json',
    transformRequest: [function (data) {
        data = Qs.stringify(data);
        return data;
    }],
    transformResponse: [function (data) {
        // 这里提前处理返回的数据
        // console.log(JSON.parse(data));
        // console.log(data);

        return data;
    }]
});

//本地存储
util.setlocalStorage = function(key, value){
    window.localStorage.setItem(key, window.JSON.stringify(value));
}
util.getlocalStorage = function(key){
    return window.JSON.parse(window.localStorage.getItem(key));
}
util.removelocalStorage = function(key){
    window.localStorage.removeItem(key);
}

//cookie 设置
util.setCookie = function(name, value , Hours) {
	if(Hours == null || Hours == ''){
		Hours = 0.5;
    }
    var exp  = new Date();
    exp.setTime(exp.getTime() + Hours*60*60*1000);
    document.cookie = name + "="+ escape (window.JSON.stringify(value)) + "; path=/;expires=" + exp.toGMTString();
}

util.getCookie = function(name) {
    var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
    if(arr=document.cookie.match(reg))
        return window.JSON.parse(unescape(arr[2]));
    else
        return null;
}

util.removeCookie = function(name) {
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval=getCookie(name);
    if(cval!=null)
        document.cookie= name + "="+cval+"; path=/;expires="+exp.toGMTString();
}

export default util;
